package com.gcd.tempe;

import com.gcd.tempe.model.Barang;
import com.gcd.tempe.util.GlobalVariabel;
import com.gcd.tempe.util.JSON_Response;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class LihatCartActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lihat_cart);
		setContentCart();
	}
	
	public void buttonOrderClick(View v){
		if (GlobalVariabel.cartItems.itemsCart.size() > 0) {
			Intent intent = new Intent(LihatCartActivity.this, MemesanBarangActivity.class);
    		startActivity(intent);
		} else {
    		Toast.makeText(LihatCartActivity.this,"Cart kosong!",Toast.LENGTH_SHORT).show();
		}
    }
	
	public void buttonClearClick(View v){
		if (GlobalVariabel.cartItems.itemsCart.size() > 0) {
			GlobalVariabel.cartItems.clearCart();
	    	Toast.makeText(LihatCartActivity.this,"Cart berhasil dikosongkan!",Toast.LENGTH_SHORT).show();

			//Back to main menu :
			Intent intent = new Intent(LihatCartActivity.this, MainTEMPE.class);
			startActivity(intent);
		} else {
			Toast.makeText(LihatCartActivity.this,"Cart kosong!",Toast.LENGTH_SHORT).show();
		}
    }
	
	public void setContentCart() {
		int sizeItemsCart = GlobalVariabel.cartItems.itemsCart.size();
		
		if (sizeItemsCart==0) {
			LinearLayout ll = (LinearLayout) findViewById(R.id.wrapperbarang);
			ll.removeAllViewsInLayout();
			LinearLayout lytContainer = new LinearLayout(this);
			lytContainer.setOrientation(LinearLayout.VERTICAL);
			
			View v = new ImageView(getBaseContext());
			ImageView image; 
			image = new ImageView(v.getContext()); 
			image.setImageDrawable(v.getResources().getDrawable(R.drawable.emptycart));
			image.setPadding(0, 30, 0, 0);
			lytContainer.addView(image);
			
			ll.addView(lytContainer);
		} else {
			LinearLayout ll = (LinearLayout) findViewById(R.id.wrapperbarang);
			ll.removeAllViewsInLayout();
			
			LinearLayout lytContainer1 = new LinearLayout(this);
			TextView poin = new TextView(this);
			poin.setText("TOTAL POIN ANDA : "+GlobalVariabel.userLoginInfo.point);
			poin.setGravity(Gravity.CENTER);
			lytContainer1.addView(poin);
			
			for (int i = 0; i < sizeItemsCart; i++) {
				LinearLayout lytContainer = new LinearLayout(this);
				lytContainer.setOrientation(LinearLayout.VERTICAL);
				
				Barang item = GlobalVariabel.cartItems.itemsCart.get(i);
				if (i % 2 == 1) {
					lytContainer.setBackgroundColor(Color.WHITE);
				} else {
					lytContainer.setBackgroundColor(Color.DKGRAY);
				}
				
				Drawable image = JSON_Response.ImageOperations(this, item.path_gambar ,"image.jpg");
				ImageView iv = new ImageView(this);
				iv.setAdjustViewBounds(true);
				iv.setMaxHeight(60);
				iv.setMaxWidth(60);
				iv.setImageDrawable(image);
				iv.setPadding(0, 10, 0, 0);
				lytContainer.addView(iv);
				
				TextView t = new TextView(this);
				t.setText(item.nama);
				t.setGravity(Gravity.CENTER);
				t.setTypeface(null, Typeface.BOLD);
				if (i % 2 == 1) {
					t.setTextColor(Color.BLACK);
				} else {
					t.setTextColor(Color.WHITE);
				}
				lytContainer.addView(t);
				
				/* TextView t2 = new TextView(this);
				t2.setText("Deskripsi : " + item.deskripsi);
				lytContainer.addView(t2);
				*/
				
				TextView t3 = new TextView(this);
				t3.setText("Harga Satuan : " +item.harga_satuan);
				t3.setGravity(Gravity.CENTER);
				if (i % 2 == 1) {
					t3.setTextColor(Color.BLACK);
				} else {
					t3.setTextColor(Color.WHITE);
				}
				lytContainer.addView(t3);
				
				TextView t4 = new TextView(this);
				t4.setText("Jumlah produk yang dipesan : " + item.jumlah);
				t4.setGravity(Gravity.CENTER);
				if (i % 2 == 1) {
					t4.setTextColor(Color.BLACK);
				} else {
					t4.setTextColor(Color.WHITE);
				}
				t4.setPadding(0, 0, 0, 10);
				lytContainer.addView(t4);
				
				ll.addView(lytContainer);
			}
		}
	}
}
